#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common import util
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtGuangDongWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)
        self.host_old_v1 = 'www.szcredit.org.cn'
        self.host_old_v2 = 'gsxt.gzaic.gov.cn'

    def get_search_result_html(self, keyword, session):
        # return [{
        #            'url': 'https://www.szcredit.org.cn/GJQYCredit/GSZJGSPTS/QYGS.aspx?rid=440301001042008031902771'}], self.SEARCH_SUCCESS
        param_list = []
        try:
            url = 'http://{host}/aiccips/'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#content', '#search', keyword, '.mianBodyStyle', proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            if jq.find('div.textStyle').find('span').text() == '0':
                return param_list, self.SEARCH_NOTHING_FIND

            item_list = jq.find('.mianBodyStyle').find('.clickStyle').find('a').items()
            for item in item_list:
                href = item.attr('href')
                if href is None or href == '':
                    continue
                if href.find('..') != -1:
                    url = 'http://{host}/aiccips'.format(host=self.host) + href[2:]
                else:
                    url = href
                param_list.append({'url': url})
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    @staticmethod
    def get_company_name_new(text):
        if text == '':
            return None
        jq = PyQuery(text, parser='html')
        company = jq.find('.conpanyInfo').find('li').eq(0).find('span').eq(0).text()

        return company.strip()

    @staticmethod
    def get_key_word_new(text):
        if text == '':
            return

        jq = PyQuery(text, parser='html')
        ent_no = jq.find('#entNo').attr('value')
        ent_type = jq.find('#entType').attr('value')
        reg_org = jq.find('#regOrg').attr('value')

        key_info = {
            'entNo': ent_no,
            'entType': ent_type,
            'regOrg': reg_org
        }

        return key_info

    # 出资信息
    def get_contributive_info_new(self, host, session, data, key_info):

        url = 'http://{host}/aiccips//invInfo/invInfoList?pageNo=1&entNo={entNo}&regOrg={regOrg}'.format(
            entNo=key_info['entNo'], regOrg=key_info['regOrg'], host=host)
        r = self.task_request(session.get, url)
        if r is None:
            return

        json_data = util.json_loads(r.text)
        if json_data is None:
            return

        # 数据可用,先发送
        self.append_model(data, Model.contributive_info, url, r.text)

        json_list = json_data.get('list', None)
        if json_list is None:
            return

        total_page = json_list.get('totalPages', 0)
        for page in xrange(2, total_page + 1):
            url = 'http://{host}/aiccips//invInfo/invInfoList?pageNo={page}&entNo={entNo}&regOrg={regOrg}'.format(
                entNo=key_info['entNo'], regOrg=key_info['regOrg'], page=page, host=host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.contributive_info, url, r.text)

        json_list = json_list.get('list', None)
        if json_list is None:
            return

        for item in json_list:
            inv_no = item.get('invNo', None)
            ent_no = item.get('entNo', None)

            url = 'http://{host}/aiccips/GSpublicity/invInfoDetails.html?invNo={invNo}&entNo={entNo}&regOrg={regOrg}'.format(
                host=host, invNo=inv_no, entNo=ent_no, regOrg=key_info['regOrg'])
            r = self.task_request(session.get, url)
            if r is None:
                continue

            self.append_model(data, Model.contributive_info, url, r.text, classify=Model.type_detail)

    # 获得主要人员信息
    def get_key_person_info_new(self, host, session, data, key_info):
        url = 'http://{host}/aiccips//vip/vipList.html?entNo={entNo}&regOrg={regOrg}'.format(
            host=host, entNo=key_info['entNo'], regOrg=key_info['regOrg'])
        r = self.task_request(session.get, url)
        if r is None:
            return

        self.append_model(data, Model.key_person_info, url, r.text)

    # 获得分支机构信息
    def get_branch_info_new(self, host, session, data, key_info):
        url = 'http://{host}/aiccips//CipBraInfo/cipBraInfoList.html?entNo={entNo}&regOrg={regOrg}'.format(
            host=host, entNo=key_info['entNo'], regOrg=key_info['regOrg'])
        r = self.task_request(session.get, url)
        if r is None:
            return

        self.append_model(data, Model.branch_info, url, r.text)

    # 股东信息
    def get_shareholder_info_new(self, host, session, data, key_info):
        url = 'http://{host}/aiccips//REIInvInfo/REIInvInfoList?pageNo=1&entNo={entNo}&regOrg={regOrg}'.format(
            entNo=key_info['entNo'], regOrg=key_info['regOrg'], host=host)
        r = self.task_request(session.get, url)
        if r is None:
            return

        json_data = util.json_loads(r.text)
        if json_data is None:
            return

        # 数据可用,先发送
        self.append_model(data, Model.shareholder_info, url, r.text)

        json_list = json_data.get('list', None)
        if json_list is None:
            return

        total_page = json_list.get('totalPages', 0)
        for page in xrange(2, total_page + 1):
            url = 'http://{host}/aiccips//REIInvInfo/REIInvInfoList?pageNo={page}&entNo={entNo}&regOrg={regOrg}'.format(
                entNo=key_info['entNo'], regOrg=key_info['regOrg'], page=page, host=host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.shareholder_info, url, r.text)

    # 获取年报信息
    def get_annual_info_new(self, host, session, data, text, key_info):
        search_list = re.findall('onclick="reportview\((.*?),.*?\)"', text)
        for year in search_list:
            url = 'http://{host}/aiccips//BusinessAnnals/annualReport.html?entNo={entNo}&reportYear={year}&entityType=1&regOrg={regOrg}'.format(
                host=host, entNo=key_info['entNo'], year=year, regOrg=key_info['regOrg'])
            r = self.task_request(session.get, url)
            if r is None:
                continue
            self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

            # 最多翻10页
            for page in xrange(1, 10):
                url = 'http://{host}/aiccips//capital/capitalList?pageNo={page}&entNo={entNo}&regOrg={regOrg}&reportYear={year}'.format(
                    host=host, page=page, entNo=key_info['entNo'], year=year, regOrg=key_info['regOrg'])
                r = self.task_request(session.get, url)
                if r is None:
                    continue
                if len(r.text) <= 15:
                    break
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    # 变更信息
    def get_change_info_new(self, host, session, data, key_info):
        url = 'http://{host}/aiccips//EntChaInfo/EntChatInfoList?pageNo=1&entNo={entNo}&regOrg={regOrg}'.format(
            entNo=key_info['entNo'], regOrg=key_info['regOrg'], host=host)
        r = self.task_request(session.get, url)
        if r is None:
            return

        json_data = util.json_loads(r.text)
        if json_data is None:
            return

        # 数据可用,先发送
        self.append_model(data, Model.change_info, url, r.text)

        json_list = json_data.get('list', None)
        if json_list is None:
            return

        total_page = json_list.get('totalPages', 0)
        for page in xrange(2, total_page + 1):
            url = 'http://{host}/aiccips//EntChaInfo/EntChatInfoList?pageNo={page}&entNo={entNo}&regOrg={regOrg}'.format(
                entNo=key_info['entNo'], regOrg=key_info['regOrg'], page=page, host=host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.change_info, url, r.text)

    # 新版方式
    def get_detail_html_list_new(self, host, seed, session, detail_url):
        # 获得基本信息
        r = self.task_request(session.get, detail_url)
        if r is None:
            return None

        company_name = self.get_company_name_new(r.text)
        if company_name is None or company_name == '':
            return None

        data = self.get_model(company_name, 0, seed, self.province)

        # 保存基础信息
        self.append_model(data, Model.base_info, detail_url, r.text)

        # 获得关键字段
        key_info = self.get_key_word_new(r.text)

        # 获得出资信息
        self.get_contributive_info_new(host, session, data, key_info)

        # 获得主要人员信息
        self.get_key_person_info_new(host, session, data, key_info)

        # 变更信息
        self.get_change_info_new(host, session, data, key_info)

        # 获得分支机构信息
        self.get_branch_info_new(host, session, data, key_info)

        # 获得股东信息
        self.get_shareholder_info_new(host, session, data, key_info)

        # 年报信息
        self.get_annual_info_new(host, session, data, r.text, key_info)

        return data

    @staticmethod
    def get_company_name_old(text):
        if text == '':
            return None
        jq = PyQuery(text, parser='html')
        company = jq.find('#CompanyInfo_EntName1').text()

        return company.strip()

    # 出资信息.attr('data')
    def get_contributive_info_old(self, session, data, text):
        if text == '':
            return

        jq = PyQuery(text, parser='html')
        item_list = jq.find('#UpdatePanel2').find('.item_box').find('td').items()
        for item in item_list:
            if item.attr('data') == 'gdjczInfo':
                info = item.attr('info')
                url = 'https://www.szcredit.org.cn/GJQYCredit/GSZJGSPTS/Detail/wucTZRInfoDetail.aspx?id=gdjczInfo&recordid={id}'.format(
                    id=info)
                r = self.task_request(session.get, url)
                if r is None:
                    continue
                self.append_model(data, Model.contributive_info, url, r.text, classify=Model.type_detail)

    def get_change_info_page_info(self, text):
        if text == '':
            return None, None, None, None

        jq = PyQuery(text, parser='html')
        __VIEWSTATEGENERATOR = jq.find('#__VIEWSTATEGENERATOR').attr('value')
        if __VIEWSTATEGENERATOR == '' or __VIEWSTATEGENERATOR is None:
            return None, None, None, None
        __EVENTVALIDATION = jq.find('#__EVENTVALIDATION').attr('value')
        if __EVENTVALIDATION == '' or __EVENTVALIDATION is None:
            return None, None, None, None
        __VIEWSTATE = jq.find('#__VIEWSTATE').attr('value')
        if __VIEWSTATE == '' or __VIEWSTATE is None:
            return None, None, None, None

        txtrid = jq.find('#txtrid').attr('value')
        if txtrid == '' or txtrid is None:
            return None, None, None, None

        return txtrid, __VIEWSTATE, __VIEWSTATEGENERATOR, __EVENTVALIDATION

    def get_split_page_info(self, text):
        text_list = text.split('</div>')
        if len(text_list) <= 0:
            return None, None

        __VIEWSTATE = None
        __EVENTVALIDATION = None

        text = text_list[-1].strip()
        text_list = text.split('|')
        length = len(text_list)

        for index, item in enumerate(text_list):
            if item == '__VIEWSTATE':
                if index + 1 < length:
                    __VIEWSTATE = text_list[index + 1]
            if item == '__EVENTVALIDATION':
                if index + 1 < length:
                    __EVENTVALIDATION = text_list[index + 1]

        return __VIEWSTATE, __EVENTVALIDATION

    # 变更信息
    def get_change_info_old(self, session, data, url, text):
        if text == '':
            return

        # 获取表单信息
        txtrid, __VIEWSTATE, __VIEWSTATEGENERATOR, __EVENTVALIDATION = self.get_change_info_page_info(text)
        if txtrid is None or __VIEWSTATE is None or __VIEWSTATEGENERATOR is None or __EVENTVALIDATION is None:
            return

        page = 1
        page_num = 1
        while page <= page_num:
            ScriptManager1 = 'UpdatePanel5|wucAlterItem$TurnPageBar1$Label{page}'.format(page=page)
            __EVENTTARGET = 'wucAlterItem$TurnPageBar1$Label{page}'.format(page=page)
            post_data = {
                'ScriptManager1': ScriptManager1,
                'TakeEntID': '',
                'txtrid': txtrid,
                'fromMail': '',
                'layerhid': 0,
                '__EVENTTARGET': __EVENTTARGET,
                '__EVENTARGUMENT': '',
                '__EVENTVALIDATION': __EVENTVALIDATION,
                '__VIEWSTATE': __VIEWSTATE,
                '__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
                '__ASYNCPOST': 'true',
            }
            page += 1
            r = self.task_request(session.post, url, data=post_data)
            if r is None:
                break

            if r.text.find('236|error|500|回发或回调参数无效') != -1:
                self.log.error('变更信息错误: {text}'.format(text=r.text))
                break

            # self.log.info(r.text)

            # 数据存储
            self.append_model(data, Model.change_info, url + '#' + Model.change_info, r.text, post_data=post_data)

            # 获得总页码数
            pattern = '共查询到.*?条信息.*?共(.*?)页'
            search_list = re.findall(pattern, text.encode('utf-8'))
            if len(search_list) <= 0:
                break

            page_num = int(search_list[0].strip())
            if page <= page_num:
                # 获取表单信息
                __VIEWSTATE, __EVENTVALIDATION = self.get_split_page_info(r.text)
                if __VIEWSTATE is None or __EVENTVALIDATION is None:
                    break

    # 获取年报信息
    def get_annual_info_old(self, session, data, text):
        if text == '':
            return
        jq = PyQuery(text, parser='html')
        item_list = jq.find('div.item_box').find('tr').items()
        for item in item_list:
            if item.text().find('年度报告') == -1:
                continue

            year_info = item.find('td').eq(1).text()
            year_list = re.findall('\d+', year_info)
            if len(year_list) <= 0:
                continue

            year = str(year_list[0])

            href = item.find('a').attr('href')
            if href is None or href == '':
                continue
            url = 'https://www.szcredit.org.cn/GJQYCredit/GSZJGSPTS/' + href
            r = self.task_request(session.get, url)
            if r is None:
                continue
            self.append_model(data, Model.annual_info, url, text, year=year, classify=Model.type_detail)

    # 旧版方式
    def get_detail_html_list_old(self, seed, session, detail_url):
        # 获得基本信息
        detail_url = detail_url.strip()
        r = self.task_request(session.get, detail_url)
        if r is None:
            return None

        company_name = self.get_company_name_old(r.text)
        if company_name is None or company_name == '':
            return None

        data = self.get_model(company_name, 0, seed, self.province)

        # 保存基础信息
        self.append_model(data, Model.base_info, detail_url + '#' + Model.base_info, r.text)

        # 出资信息
        self.get_contributive_info_old(session, data, r.text)

        # 变更信息
        self.get_change_info_old(session, data, detail_url, r.text)

        # 年报信息
        self.get_annual_info_old(session, data, r.text)

        return data

    def get_detail_html_list(self, seed, session, param_list):

        data_list = []
        rank = 0
        for item in param_list:
            try:
                detail_url = item.get('url', None)
                data = None
                if detail_url.find(self.host) != -1:
                    data = self.get_detail_html_list_new(self.host, seed, session, detail_url)
                elif detail_url.find(self.host_old_v1) != -1:
                    data = self.get_detail_html_list_old(seed, session, detail_url)
                elif detail_url.find(self.host_old_v2) != -1:
                    data = self.get_detail_html_list_new(self.host_old_v2, seed, session, detail_url)
                else:
                    self.log.error('url 无法解析: {url}'.format(url=detail_url))
                if data is not None:
                    rank += 1
                    data['rank'] = rank
                    data_list.append(data)
            except Exception as e:
                self.log.exception(e)
        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list)
